/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.WebServiceClient;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="WSC_insertDoctor",
                urlPatterns={"/WebServiceClient/WSC_insertDoctor",
                "/WebServiceClient/WSC_insertDoctor/formMDoctor_Submit"})
public class WSC_insertDoctor extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/suntiparb-pc_9484/insertDoctorService/insertDoctorPort.wsdl")
    private InsertDoctorService service;
   
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/WebServiceClient/WSC_insertDoctor")) {
            response.setContentType("text/html;charset=utf-8");

            userPath = "/WebServiceClient/WSC_insertDoctor";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();


        if (userPath.equals("/WebServiceClient/WSC_insertDoctor/formMDoctor_Submit")) {

//            String DoctorId = (request.getParameter("DoctorId") != null) ? request.getParameter("DoctorId") : "-1";
            String txtName = (request.getParameter("txtName") != null) ? request.getParameter("txtName") : "";
            String txtSurname = (request.getParameter("txtSurname") != null) ? request.getParameter("txtSurname") : "";
            String rdbSex = (request.getParameter("rdbSex") != null) ? request.getParameter("rdbSex") : "";
            String txtCode = (request.getParameter("txtCode") != null) ? request.getParameter("txtCode") : "";
            //Change rdbSex Value to Boolean
            if (rdbSex.equals("Male")){
                rdbSex = "true";
            }else{
                rdbSex = "false";
            }

            PrintWriter out = response.getWriter();
            try {
                MDoctorRecord listDoctor = new MDoctorRecord();
                MDoctorRecord.Name tmpname = new MDoctorRecord.Name();
                MDoctorRecord.Surname tmpsurname = new MDoctorRecord.Surname();
                MDoctorRecord.Sex tmpsex = new MDoctorRecord.Sex();
                MDoctorRecord.Code tmpcode = new MDoctorRecord.Code();
                tmpname.setValue(txtName);
                tmpsurname.setValue(txtSurname);
                tmpsex.setValue(Boolean.parseBoolean(rdbSex));
                tmpcode.setValue(txtCode);
                listDoctor.setName(tmpname);
                listDoctor.setSurname(tmpsurname);
                listDoctor.setSex(tmpsex);
                listDoctor.setCode(tmpcode);
//                listDoctor.name.setValue(txtName);
//                listDoctor.surname.setValue(txtSurname);
//                listDoctor.sex.setValue(Boolean.parseBoolean(rdbSex));
//                listDoctor.code.setValue(txtCode);
                MDoctor inputData = new MDoctor();
                inputData.getMDoctorRecord().add(0, listDoctor);
                String Message = insertDoctorOperation(inputData);
                out.println(Message);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
            }

            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }

    private String insertDoctorOperation(controller.WebServiceClient.MDoctor inputData) {
        controller.WebServiceClient.InsertDoctorPortType port = service.getInsertDoctorPort();
        return port.insertDoctorOperation(inputData);
    }
}
